const app = getApp()
import api from '../../utils/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //等待框
    waitHidden: true,
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
    //页面尺寸数据
    windowHeight: 0,
    screenHeight: 0,
    feedbackId: 0,
    feedbackDetail: [],
    show: false,
    quoteText: "",
    replyFeedback: "",
    idx: "",
    sendFlag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this
    wx.hideShareMenu()
    //设置屏幕尺寸数据
    this.setData({
      windowHeight: app.globalData.windowHeight,
      screenHeight: app.globalData.screenHeight
    })
    /**
     * 通过传过来的id获取feedback
     */
    if(options.feedbackId){
      _this.setData({
        feedbackId: options.feedbackId
      })
    }
    if(options.idx){
      _this.setData({
        idx: options.idx
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const _this = this
    wx.request({
      url: api.cloudGetDetails,
      method: "POST",
      data: {
        id: _this.data.feedbackId
      },
      header: {
      'content-type': 'application/json' // 默认值
      },
      success(res) {
        _this.setData({
          feedbackDetail: res.data.data
        })
      }
    })
    /*
    * 更新为已读状态
    */
    wx.request({
      url: api.cloudUpdateQuestionReadStatus,
      method: "POST",
      data: {
        id: _this.data.feedbackId,
        manager: false
      },
      header: {
      'content-type': 'application/json' // 默认值
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  goBack: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 跳转到首页
   */
  goToIdx: function (e) {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
  },
  reply: function (e){
    if (e.currentTarget.dataset.feedbackcontent.length <= 15) {
      this.setData({
        show: true,
        quoteText: e.currentTarget.dataset.feedbackcontent
      })
    } else {
      this.setData({
        show: true,
        quoteText: e.currentTarget.dataset.feedbackcontent.substring(0,15) + "..."
      })
    }
  },
  previewImage: function(e){
    wx.previewImage({
      urls: [e.currentTarget.dataset.imgurl],
    })
  },
  hideTextArea: function(){
    this.setData({
      show: false
    })
  },
  /**
   * 绑定输入框文字
   */
  commentInput: function (e) {
    this.setData({
      replyFeedback: e.detail.value
    })
  },
  sendClick: function () {
    var that = this
    if (this.data.replyFeedback == "") {
      wx.showToast({
          title: '请输入反馈',
          icon: 'error'
      })
    } else {
      if (this.data.sendFlag) {
        this.setData({
          sendFlag: false
        })
        wx.request({
          url: api.cloudAddFeedback,
          method: "POST",
          data: {
            moduleType: that.data.feedbackDetail[0].moduleType,
            content: that.data.replyFeedback,
            id: that.data.feedbackDetail[0].id,
            userType: wx.getStorageSync('logindata').data.userType,
            pagepath: '/pages/feedbackDetailReply/feedbackDetailReply?idx=true&feedbackId=' + that.data.feedbackDetail[0].id
          },
          header: {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token')
          },
          success(e) {
            if (e.statusCode == 200) {
              wx.showToast({
                title: '提交反馈成功！',
              })
              that.onShow()
            } else {
              wx.showToast({
                  title: '提交反馈失败' + e.statusCode,
                  icon: 'error'
              })
            }
            that.setData({
              sendFlag: true
            })
          }
        })
      }
    }
  }

})